<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>{$game.name}</title>
    <link rel="stylesheet" href="__CSS__/index.css">
    <link rel="stylesheet" href="__CSS__/font-awesome.min.css">

    <link rel="stylesheet" href="__JS__/layui/css/layui.css" media="all">

</head>
<body>
    <div class="bg-container" {if condition="!empty($game['Bgimg'])"}style="background: url('{$game.Bgimg}') no-repeat center;"{/if}></div>
    <div id="game-container" {if condition="!empty($game['Bgimg'])"}style="margin-top: 8%;height: 96.5%;" {/if}>
        <div class="loading">游戏加载中...</div>
        <iframe {if condition="$game.id == 17"}allow="autoplay; camera; fullscreen; geolocation; microphone; payment; midi; encrypted-media"{/if} id="game-frame" src="{$game.api_login_url}"></iframe>
    </div>

    <div class="float-ball" id="floatBall" style="display: none;">
        <span></span>
    </div>
    <script src="__JS__/jquery.min.js"></script>
    <script src="__JS__/layui/layui.all.js"></script>
    <script src="__JS__/game/commom.js"></script>
    <script src="__JS__/game/login.js"></script>
    <script src="__JS__/game/register.js"></script>
    <script src="__JS__/game/forgot_pwd.js"></script>
    <script src="__JS__/game/change_pwd.js"></script>
    <script src="__JS__/game/player_center.js"></script>
    <script src="__JS__/game/customer_service.js"></script>
    <script src="__JS__/game/pay.js"></script>
    <script src="__JS__/game/channel_pay.js"></script>

{/*    <script src="https://unpkg.com/vconsole@latest/dist/vconsole.min.js"></script>*/}

    <script>
        //url初始化
        const FromUrl = "{:url('/game/index')}";//登录弹窗
        const phoneLoginUrl = "{:url('/game/phoneLogin')}";//手机号登录
        const sendSmsUrl = "{:url('/game/sendSms')}";//发送验证码
        const accountLoginUrl = "{:url('/game/login')}";//账号登录
        const registerUrl = "{:url('/game/register')}";//注册
        const forgetPwdUrl = "{:url('/game/forgetPassword')}";//忘记密码
        const changePasswordUrl = "{:url('/game/changePassword')}";//修改密码
        const logoutUrl = "{:url('/game/logout')}";//退出登录
        const payUrl = "{:url('/game/pay')}";//支付
        const createRoleUrl = "{:url('/game/createRole')}";//创建角色
        const uploadLoginTimeUrl = "{:url('/game/uploadLoginTime')}";//更新登录信息
        const customerCervice = "{:url('/game/customerCervice')}";//玩家中心
        const uploadImg = "{:url('/game/uploadImg')}";//图片上传图片
        const submitReport = "{:url('/game/submitReport')}";//提交举报
        const getPlayerMsg = "{:url('/game/getPlayerMsg')}";//获取玩家消息
        const sendPlayerMsg = "{:url('/game/sendPlayerMsg')}";//发送玩家消息
        const getOrderQuery = "{:url('/game/order/query')}";//订单状态查询
        const getWxLoginUrl = "{:url('/game/getWxLogin')}";// 微信登录参数
        const getWxConfigUrl = "{:url('/game/getWxConfig')}";// 微信配置参数
        const playerAuthUrl = "{:url('/game/playerAuth')}";// 实名认证

        const uid = "{$uid}";
        const gid = "{$game.id}";
        const cid = "{$cid}";
        let token = "{$token}";
        const is_fcm = "{$showFCM}";
        console.log("token:",token);

        console.log("防沉迷弹窗:",is_fcm);
        // 实名认证
        let is_age = "{$age_status}";
        console.log("实名认证信息:",is_age);
        let uploadLoginTime = null;

        let is_aiqi_cid = 1;
        let msg_id = 0;

        if(!token){
            if(isWeixin()){
                $.get(getWxConfigUrl,{gid:gid,cid:cid},function(info) {
                    if(info.code == 1){
                        window.location.href = info.data;
                    }
                });
            }else{
                showLoginModal();
            }
        }

        if(cid == "5"){
            if(isiOS()){
                loadScript("https://g.alicdn.com/code/npm/@ali/act-lib/0.1.8/index.js");
            }else{
                loadScript("https://g.alicdn.com/code/npm/@ali/amap-lib/7.3.25/index.js");
            }
        }

        if(is_fcm == 1){
            showLoginOutModal();
        }
        if(gid == 10 || uid == 32){
            // showVconsole();
        }
        // 模拟上层环境的方法
        // 父窗口代码
        window.addEventListener('message', (event) => {
        // 安全考虑：验证消息来源
        // if (event.origin !== "https://your-iframe-domain.com") return;
            console.log('接收到更新111:', event);

        // 检查消息类型
        if (event.data && event.data.requestType === 'payment') {
            event.data.params.token = token;
            console.log('支付数据:', event.data.params);
            if(cid == 6){
                 // 发行下单
                getChannelOrder(event.data.params);
            }else{
                showPaymentModal(event.data.params);
            }
        }
        if (event.data && event.data.requestType === 'create_role' && token) {
            event.data.params.token = token;
            event.data.params.cid = cid;
            console.log('创建角色数据:', event.data.params);
            $.post(createRoleUrl,event.data.params,function(info) {
                console.log("创建角色结果:",info);
            });
        }
        });


        // 动态加载js文件
        function loadScript(url) {
            const script = document.createElement('script');
            script.type = 'text/javascript';
            script.src = url;

            script.onerror = function() {
                console.error('Error loading script: ' + url);
            };

            document.head.appendChild(script);
        }

        // 检查登录状态
        function checkLoginStatus() {
            if(token){
                return true;
            }
            return false;
        }

        function showVconsole() {
            // 初始化 vConsole
            var vConsole = new VConsole();

            class ReloadPlugin extends VConsole.VConsolePlugin {
                constructor() {
                    super('reload', '刷新');
                }
                onRenderTab(callback) {
                    // 创建按钮
                    var btn = document.createElement('button');
                    btn.innerText = '重新加载页面';
                    btn.style.margin = '10px';
                    btn.onclick = function() {
                        location.reload(true);
                    };
                    // 创建容器
                    var container = document.createElement('div');
                    container.appendChild(btn);
                    // 渲染
                    callback(container);
                }
            }
            vConsole.addPlugin(new ReloadPlugin());
        }

        // 页面加载完成后检查登录状态
        window.onload = function() {
            console.log('页面加载完成');
            const gameFrame = document.getElementById('game-frame');
            const loading = document.querySelector('.loading');
            // 页面加载时清除定时器
            if (timer) {
                clearInterval(timer);
                timer = null;
            }
            if(cid == 5){
                console.log('在⾼德端内...');
            }else{
                is_aiqi_cid = 0;
                console.log('不在⾼德端内...');
            }

            if (checkLoginStatus()) {
                // 实名认证
                if(is_age == 0 && cid != 5){
                    showAgeModal();
                }

                //  高德数据上报 定时发送心跳包（每20秒）
                uploadLoginTime = setInterval(() => {
                    // 获取当前URL的查询参数
                    const urlParams = new URLSearchParams(window.location.search);
                    const rp_token = urlParams.get('rp_token');
                    // 这里添加修改密码API调用
                    const data = {
                        uid: uid,
                        gid: gid,
                        cid: cid,
                        rp_token: rp_token,
                        token: token,
                    };
                    $.post(uploadLoginTimeUrl, data, function(info) {
                        if(info.code == 0){
                            console.log("数据上报返回:",info.code);
                            // 清除定时器
                            clearInterval(uploadLoginTime);
                            // 重新加载页面
                            location.reload();
                        }
                    });
                }, 5000);
                if(token){
                    const url = new URL(window.location.href);
                    url.searchParams.set("token", token);
                    window.history.replaceState(null, '', url);
                }
                // 显示游戏
                gameFrame.style.display = 'block';
                showFloatBall();
                loading.style.display = 'none';
                // 福利弹窗
                shouyeArticle(0,1);
            }
            // 点击其他地方关闭下拉框
            document.addEventListener('click', (e) => {
                const accountOptions = document.getElementById('accountOptions');
                if (!e.target.closest('.account-select') && accountOptions) {
                    accountOptions.style.display = 'none';
                }
                const phoneOptions = document.getElementById('phoneOptions');

                if (!e.target.closest('.phone-select') && phoneOptions) {
                    phoneOptions.style.display = 'none';
                }
            });

            // 启用拖拽功能
            const floatBall = document.getElementById('floatBall');
            if (floatBall && isMobile()) {
                enableDrag(floatBall);
            }
        };
    </script>
</body>
</html>